﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using FirstFloor.ModernUI.Windows.Navigation;
using LinnerToolkit.Desktop.ModernUI.Mvvm;
using LinnerToolkit.Desktop.ModernUI.Navigation;
using SimulationPTSystem.DAL;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace SimulationPTSystem.ViewModels
{
    public class TeamInfoPageViewModel : ModernViewModelBase
    {
        private string teamCode;
        private string teamName;
        private int pageIndex;
        private ObservableCollection<ChengYuanInfo> removeChengYuanInfos;
        private ObservableCollection<ChengYuanInfo> addChengYuanInfos;
        private TeamInfo editTeamInfo;
        private TeamInfo selectedTeamInfo;
        private ObservableCollection<TeamInfo> teamInfos;
        private string queryChengYuanName;

        /// <summary>
        /// 每页人员数量
        /// </summary>
        const int PageSize = 20;

        /// <summary>
        /// 查询的团队编号
        /// </summary>
        public string TeamCode { get => teamCode; set => Set(ref teamCode, value); }
        /// <summary>
        /// 查询的团队名称
        /// </summary>
        public string TeamName { get => teamName; set => Set(ref teamName, value); }
        /// <summary>
        /// 查询到的团队信息集合
        /// </summary>
        public ObservableCollection<TeamInfo> TeamInfos { get => teamInfos; set => Set(ref teamInfos, value); }
        /// <summary>
        /// 选中的团队信息
        /// </summary>
        public TeamInfo SelectedTeamInfo { get => selectedTeamInfo; set => Set(ref selectedTeamInfo, value); }
        /// <summary>
        /// 待编辑的团队信息
        /// </summary>
        public TeamInfo EditTeamInfo { get => editTeamInfo; set => Set(ref editTeamInfo, value); }
        /// <summary>
        /// 已添加到团队的人员信息集合
        /// </summary>
        public ObservableCollection<ChengYuanInfo> AddChengYuanInfos { get => addChengYuanInfos; set => Set(ref addChengYuanInfos, value); }
        /// <summary>
        /// 未添加到团队的人员信息集合
        /// </summary>
        public ObservableCollection<ChengYuanInfo> RemoveChengYuanInfos { get => removeChengYuanInfos; set => Set(ref removeChengYuanInfos, value); }
        /// <summary>
        /// 当前页面号
        /// </summary>
        public int PageIndex { get => pageIndex; set => Set(ref pageIndex, value); }

        /// <summary>
        /// 模糊查询的人员姓名
        /// </summary>
        public string QueryChengYuanName
        {
            get => queryChengYuanName;
            set
            {
                if(Set(ref queryChengYuanName, value))
                {
                    PageIndex = 1;
                    RemoveChengYuanInfos = new ObservableCollection<ChengYuanInfo>(SimulationPTDAL.GetChengYuanInfos(PageIndex, PageSize, value));
                }
            }
        }

        /// <summary>
        /// 查询命令
        /// </summary>
        public ICommand QueryCommand { get; }
        /// <summary>
        /// 清除命令
        /// </summary>
        public ICommand ClearCommand { get; }
        /// <summary>
        /// 查看详细信息命令
        /// </summary>
        public ICommand DetailsCommand { get; }
        /// <summary>
        /// 添加人员命令
        /// </summary>
        public ICommand AddCommand { get; }
        /// <summary>
        /// 删除人员命令
        /// </summary>
        public ICommand DeleteCommand { get; }
        /// <summary>
        /// 保存命令
        /// </summary>
        public ICommand SaveCommand { get; }
        /// <summary>
        /// 取消命令
        /// </summary>
        public ICommand CancelCommand { get; }

        /// <summary>
        /// 添加当前页成员命令
        /// </summary>
        public ICommand AddCurrentPageChengYuanInfosCommand { get; }
        /// <summary>
        /// 添加选中成员命令
        /// </summary>
        public ICommand AddSelectedChengYuanInfosCommand { get; }
        /// <summary>
        /// 移除选中成员命令
        /// </summary>
        public ICommand RemoveSelectedChengYuanInfosCommand { get; }
        /// <summary>
        /// 上一页命令
        /// </summary>
        public ICommand PrevPageCommand { get; }
        /// <summary>
        /// 下一页命令
        /// </summary>
        public ICommand NextPageCommand { get; }

        public TeamInfoPageViewModel(IModernNavigationService navigationService) : base(navigationService)
        {
            AddChengYuanInfos = new ObservableCollection<ChengYuanInfo>();
            RemoveChengYuanInfos = new ObservableCollection<ChengYuanInfo>();

            QueryCommand = new RelayCommand(async (obj) =>
            {
                TeamInfos = new ObservableCollection<TeamInfo>(await SimulationPTDAL.GetTeamInfosAsync(TeamCode, TeamName));
            });

            ClearCommand = new RelayCommand((obj) =>
              {
                  TeamCode = TeamName = string.Empty;
              });

            DetailsCommand = new RelayCommand((obj) =>
              {
                  if (SelectedTeamInfo == null)
                  {
                      ModernDialog.ShowMessage("请先选择要查看的团队", string.Empty, System.Windows.MessageBoxButton.OK);
                      return;
                  }

                  ResetEditTeamInfo(SelectedTeamInfo);
              });

            AddCommand = new RelayCommand((obj) =>
            {
                ResetEditTeamInfo();
            });

            DeleteCommand = new RelayCommand((obj) =>
            {
                if (SelectedTeamInfo == null)
                {
                    ModernDialog.ShowMessage("请先选择要删除的团队", string.Empty, System.Windows.MessageBoxButton.OK);
                    return;
                }

                if (ModernDialog.ShowMessage("是否要删除所选团队？", "提示", System.Windows.MessageBoxButton.OKCancel) == System.Windows.MessageBoxResult.OK)
                {
                    SimulationPTDAL.DeleteTeamInfo(SelectedTeamInfo);
                    TeamInfos.Remove(SelectedTeamInfo);
                }
            });

            SaveCommand = new RelayCommand((obj) =>
            {
                if (string.IsNullOrEmpty(EditTeamInfo.Code))
                {
                    ModernDialog.ShowMessage("团队编号不能为空", string.Empty, System.Windows.MessageBoxButton.OK);
                    return;
                }

                if (string.IsNullOrEmpty(EditTeamInfo.Name))
                {
                    ModernDialog.ShowMessage("团队名称不能为空", string.Empty, System.Windows.MessageBoxButton.OK);
                    return;
                }

                if (SelectedTeamInfo != null && EditTeamInfo.Id == SelectedTeamInfo.Id)
                {
                    SelectedTeamInfo.Code = EditTeamInfo.Code;
                    SelectedTeamInfo.Name = EditTeamInfo.Name;
                    SelectedTeamInfo.ChengYuanInfos = AddChengYuanInfos;
                    SelectedTeamInfo.ChengYuanCount = AddChengYuanInfos.Count;

                    SimulationPTDAL.UpdateTeamInfo(SelectedTeamInfo);
                }
                else
                {
                    EditTeamInfo.ChengYuanInfos = AddChengYuanInfos;
                    EditTeamInfo.ChengYuanCount = AddChengYuanInfos.Count;
                    SimulationPTDAL.AddTeamInfo(EditTeamInfo);
                }

                EditTeamInfo = null;
            });

            CancelCommand = new RelayCommand((obj) =>
            {
                EditTeamInfo = null;
            });

            AddCurrentPageChengYuanInfosCommand = new RelayCommand((obj) =>
             {
                 if (RemoveChengYuanInfos?.Count <= 0)
                     return;

                 foreach (var chengYuanInfo in RemoveChengYuanInfos)
                 {
                     if (!AddChengYuanInfos.Contains(chengYuanInfo))
                         AddChengYuanInfos.Add(chengYuanInfo);
                 }
             });

            AddSelectedChengYuanInfosCommand = new RelayCommand((chengYuanInfos) =>
              {
                  if (chengYuanInfos is IList list)
                  {
                      if (list.Count <= 0)
                          return;

                      foreach (var chengYuanInfo in list.Cast<ChengYuanInfo>().ToList())
                      {
                          if (!AddChengYuanInfos.Contains(chengYuanInfo))
                              AddChengYuanInfos.Add(chengYuanInfo);
                      }
                  }
              });

            RemoveSelectedChengYuanInfosCommand = new RelayCommand((chengYuanInfos) =>
             {
                 if (chengYuanInfos is IList list)
                 {
                     if (list.Count <= 0)
                         return;

                     foreach (var chengYuanInfo in list.Cast<ChengYuanInfo>().ToList())
                         AddChengYuanInfos.Remove(chengYuanInfo);
                 }
             });

            PrevPageCommand = new RelayCommand((obj) =>
             {
                 if (PageIndex > 1)
                     RemoveChengYuanInfos = new ObservableCollection<ChengYuanInfo>(SimulationPTDAL.GetChengYuanInfos(--PageIndex, PageSize, QueryChengYuanName));
             }, (obj) => PageIndex > 1);
            NextPageCommand = new RelayCommand((obj) =>
             {
                 RemoveChengYuanInfos = new ObservableCollection<ChengYuanInfo>(SimulationPTDAL.GetChengYuanInfos(++PageIndex, PageSize, QueryChengYuanName));
             }, (obj) => RemoveChengYuanInfos.Count == PageSize);
        }

        private void ResetEditTeamInfo(TeamInfo teamInfo = null)
        {
            if (teamInfo == null)
            {
                EditTeamInfo = new TeamInfo();
            }
            else
            {
                EditTeamInfo = new TeamInfo
                {
                    Id = teamInfo.Id,
                    Code = teamInfo.Code,
                    Name = teamInfo.Name,
                    ChengYuanInfos = teamInfo.ChengYuanInfos,
                    ChengYuanCount = teamInfo.ChengYuanCount,
                };
            }

            if (EditTeamInfo.ChengYuanInfos == null || EditTeamInfo.ChengYuanInfos.Count <= 0)
                AddChengYuanInfos = new ObservableCollection<ChengYuanInfo>();
            else
                AddChengYuanInfos = new ObservableCollection<ChengYuanInfo>(EditTeamInfo.ChengYuanInfos);
            if (PageIndex <= 0)
            {
                PageIndex = 1;
                RemoveChengYuanInfos = new ObservableCollection<ChengYuanInfo>(SimulationPTDAL.GetChengYuanInfos(PageIndex, PageSize, QueryChengYuanName));
            }
        }

        public override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            PageIndex = 0;
            QueryChengYuanName = string.Empty;
        }

        public override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            EditTeamInfo = null;
        }
    }
}
